package com.jumei.flume.processor;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.jumei.flume.bo.SensorMessage;
import com.jumei.flume.processor.rules.*;

import java.util.*;

/**
 * 添加页面等级标示
 * <p>
 * 注意：
 * 1、规则有先后顺序，暂时通过TypeResolver2.rank()来排序
 * 2、规则链运行规则：规则命中后，即退出，不在后续执行
 *
 * @author admin
 */
public class PageTypeResolver {

    public static void main(String[] args) {
        LinkedList<String> a = Lists.newLinkedList();
        a.add("11");
        a.add("2");
        a.add("3");
        a.addLast("5");
        a.addFirst("0");
        a.addFirst("-1");
        a.addLast("7");

        System.out.println(JSON.toJSONString(a));

        Collections.sort(a, new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                return Integer.valueOf(o2) - Integer.valueOf(o1);
            }

        });

        System.out.println(JSON.toJSONString(a));


//		List<TypeResolver> l = PageTypeResolver.getGeneralRules();
//		System.out.println(l.size());
//		
//		List<TypeResolver2> l2 = PageTypeResolver.getReferrerRules();
//		System.out.println(l2.size());
//		
//		List<TypeResolver2> l3 = PageTypeResolver.getUrlRules();
//		System.out.println(l3.size());

        List<TypeResolver2> l4 = PageTypeResolver.getClickNameRules();
        System.out.println(l4.size());

        List<TypeResolver2> l5 = PageTypeResolver.getViewNameRules();
        System.out.println(l5.size());
    }

    /**
     * 其他事件规则解析
     *
     * @return
     */
    public static List<TypeResolver> getGeneralRules() {
        LinkedList<TypeResolver> resolver = Lists.newLinkedList();
        resolver.addAll(GeneralRules.resolvers.values());

        Collections.sort(resolver, new Comparator<TypeResolver>() {

            @Override
            public int compare(TypeResolver o1, TypeResolver o2) {
                return o1.rank() - o2.rank();
            }

        });

        return resolver;

    }

    /**
     * $referrer规则解析
     *
     * @return
     */
    public static List<TypeResolver2> getReferrerRules() {
        LinkedList<TypeResolver2> resolver = Lists.newLinkedList();
        resolver.addAll(ReferrerRules.view_resolvers.values());

        Collections.sort(resolver, new Comparator<TypeResolver2>() {

            @Override
            public int compare(TypeResolver2 o1, TypeResolver2 o2) {
                return o1.rank() - o2.rank();
            }

        });

        return resolver;

    }

    /**
     * $url规则解析
     *
     * @return
     */
    public static List<TypeResolver2> getUrlRules() {
        LinkedList<TypeResolver2> resolver = Lists.newLinkedList();
        resolver.addAll(UrlRules.view_resolvers.values());

        Collections.sort(resolver, new Comparator<TypeResolver2>() {

            @Override
            public int compare(TypeResolver2 o1, TypeResolver2 o2) {
                return o1.rank() - o2.rank();
            }

        });

        return resolver;
    }

    /**
     * 添加曝光点击来源的中英文名称
     *
     * @return
     */
    public static List<TypeResolver2> getClickNameRules() {

        return getRulesLists(AddClickNameRules.name_resolvers.values());
    }

    public static List<TypeResolver2> getViewNameRules() {

        return getRulesLists(AddViewNameRules.view_name_resolvers.values());
    }


    private static List<TypeResolver2> getRulesLists(Collection<TypeResolver2> c) {

        LinkedList<TypeResolver2> resolver = Lists.newLinkedList();
        resolver.addAll(c);

        Collections.sort(resolver, new Comparator<TypeResolver2>() {

            @Override
            public int compare(TypeResolver2 o1, TypeResolver2 o2) {
                return o1.rank() - o2.rank();
            }

        });

        return resolver;

    }


    /**
     * 规则路由
     *
     * @param record
     * @param context
     */
    public static void route(SensorMessage record, ProcessorContext context) {

    }

}